// Όλα τα global αντικείμενα της εφαρμογής δημιουργούνται ως
// properties ενός μόνο πραγματικά global αντικειμένου.

var pineza = {};

function init() {
	pineza.keepAliveRepeatTime = 60000;
	var x = getelid('bottom');
	if (x && x.style) {
		x.style.display = 'inline';
	}

	if (notSet(pineza.login)) {
		x = getelid('boardTab');
	}

	pineza.colorError = '#FF0000';
	pineza.colorOk = '#000000';

	pineza.avoidCacheBase = currentTimestamp();
	pineza.avoidCacheDif = 0;

	var x = new Date;
	x = parseInt(x.getTime() / 1000);
	pineza.timeDif -= x;
}

window.onload = init;

function isnum(x) {
	return (x.match(/^[0-9]+$/));
}

String.prototype.trim = function() {
	return (this.replace(/^\s+|\s+$/g,""));
}

String.prototype.ltrim = function() {
	return (this.replace(/^\s+/,""));
}

String.prototype.rtrim = function() {
	return (this.replace(/\s+$/,""));
}

function isSet(x) {
	if (x === undefined) return false;
	if (x === null) return false;
	return true;
}

function notSet(x) {
	return (!isSet(x));
}

function currentTimestamp(db) {
	var x = new Date();
	if (isSet(db)) {
		 var x = (x.getFullYear() * 10000000000) +
			((x.getMonth() + 1) * 100000000) +
			(x.getDate() * 1000000) +
			(x.getHours() * 10000) +
			(x.getMinutes() * 100) +
			x.getSeconds();
	}
	else {
		x = x.getTime();
	}

	return x;
}

function getelid(id) {
	return(document.getElementById(id));
}

function keepAlive() {
	var req = newRequest('keepAlive');
	if (req) {
		req.open('post', pineza.server + 'lib/keepAlive.php', true);
		req.onreadystatechange = function() {
			keepAliveCheck(req);
		};
		var params = 'login=' + uri(pineza.login);
		sendRequest(req, params);
	}
	else {
		keepAliveError('failed AJAX request');
	}
}

function keepAliveCheck(req) {
	if (req.readyState != 4) {
		return;
	}

	setTimeout(keepAlive, pineza.keepAliveRepeatTime);
	if (req.status != 200) {
		keepAliveError('status = ' + (isSet(req.status) ? req.status : 'unknown status'));
		return;
	}

	var rsp = fixResponse(req);
	if (rsp) {
		if (pineza.keepAliveErrorCount == undefined)
			pineza.keepAliveErrorCount = 0;
		else if (pineza.keepAliveErrorCount++ > 0)
			keepAliveError('remote response: ' + rsp);
	}
}
	
function keepAliveError(msg) {
	if (!isSet(msg)) msg = '*** connection error ***';
	var x = getelid('alive');
	if (x) x.innerHTML = '<span class="lathos alive" title="Explain error" onclick="alert(\'' + msg +
		'\');">*** lost connection ***</span>';

	/* Ξαναφορτώνω την αρχική σελίδα */
	setTimeout(function() {
		parent.location = pineza.server + 'login/index.php?login=' +
			uri(pineza.login) + '&lostcon=yes';
	}, 1000);
}

function newRequest(msg) {
	var xhr = false;
	try {
		xhr = new XMLHttpRequest();
	} catch (err) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (err) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				xhr = false;
			}
		}
	}

	return(xhr);
}

function sendRequest(xhr, data) {
	xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

	if (notSet(data)) data = '';
	else if (data != '') data += '&';
	data += 'avoidCache=' + pineza.avoidCacheBase + ':' + pineza.avoidCacheDif++;

	xhr.send(data);
}

function fixResponse(req) {
	if (isSet(req) && isSet(req.responseText)) {
		var s = req.responseText.split(pineza.ajaxSeparator)
		return(s[0]);
	}
	return 'undefined Ajax response';
}

function uri(s) {
	return(encodeURIComponent(s));
}

function apolitaXY(obj) {
	var XY = new Object;
	XY.x = 0;
	XY.y = 0;

	while (obj.offsetParent) {
		XY.x += obj.offsetLeft;
		XY.y += obj.offsetTop;
		obj = obj.offsetParent;
	}

	return(XY);
}

function boardSelect() {
	return false;
}

function newNote(ps) {
	var x = ps.document.getElementById('board');
	if (!x) return false;

	var w = x.contentWindow;
	if (!w) return false;

	if (!isSet(w.pineza.board)) {
		alert('board undefined');
		return false;
	}
	pineza.board = w.pineza.board;

	if (x.contentDocument)
		x = x.contentDocument;
	else if (x.contentWindow)
		x = x.contentWindow.document;
	else
		return false;

	f = x.getElementById('formaNotePosition');
	if (f) {
		var fn = formaNoteHTML();
		if (fn.match(/^ERROR@/)) {
			f.innerHTML = '<span class="lathos">' +
				fn.replace(/^ERROR@/, '') + '</span>';
			setTimeout(function() {
				f.innerHTML = '';
			}, 1000);
			return false;
		}

		f.innerHTML = fn;
		f = x.getElementById('formaNote');
		if (f) f.kimeno.focus();
	}

	return false;
}

function formaNoteHTML(note) {
	var req = newRequest('formaNote');
	req.open('post', pineza.server + 'board/formaNote.php', false);
	var params = 'login=' + uri(pineza.login);
	if (isSet(note)) params += '&note=' + uri(note);
	else params += '&board=' + uri(pineza.board);
	sendRequest(req, params);
	return(fixResponse(req));
}

function workingBoardSB(board) {
	var sbf = parent.document.getElementById('boardSB');
	if (!sbf) return false;

	var doc = sbf.contentWindow.document;
	var x = doc.getElementById('SBmb' + board);
	if (x) {
		x.innerHTML = '<img src="' + pineza.server +
			'images/working.gif" class="workingBoard" alt="working icon" />';
	}
}

function refreshBoardSB(board) {
	var x = parent.document.getElementById('boardSB');
	if (!x) return false;

	if (isSet(board)) {
		delete x.contentWindow.pineza.boardHTML[board];
		var doc = x.contentWindow.document;
		var x = doc.getElementById('SBmb' + board);
		if (x) {
			var req = newRequest('mikrografies');
			req.open('post', pineza.server + 'boardSB/mikrografies.php', true);
			req.onreadystatechange = function() {
				if (req.readyState != 4)
					return;

				x.innerHTML = (req.status == 200 ? fixResponse(req) : '*** error ***');
			};

			var params = 'login=' + uri(pineza.login) + '&board=' + uri(board);
			sendRequest(req, params);
		}
	}
	else {
		x.src = pineza.server + 'boardSB/index.php';
	}
}

function validEmail(email) {
	if (email === undefined) return false;
	if (email === null) return false;
	return email.match(/^[a-zA-Z0-9_\.-]+\@([a-zA-Z0-9-]+\.)+([a-zA-Z0-9]{2,4})+$/);
}

function debug(msg, id) {
	if (!isSet(id)) id = 'debug';
	var dbg = getelid(id);
	if (!dbg) return false;

	if (!isSet(msg))
		dbg.innerHTML = '<span style="color: grey; font-style: italic;">DEBUG</span>';
	else
		dbg.innerHTML += '>' + msg;

	return true;
}
